//
//  WPCHomepagePresenter.m
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/4.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

#import "WPCHomepagePresenter.h"
#import "WPCNetWorkingManager.h"
#import "WPCThemesInfo.h"
#import "WPCHomepageViewController.h"
#import "WPCURLFactory.h"
#import "WPCPreviewViewController.h"
#import "WPCCategoryModel.h"
#import "Wallpaper_co-Swift.h"

@interface WPCHomepagePresenter()
@property (nonatomic, strong)   WPCThemesModel  *themesModel;
@property (nonatomic, weak)     WPCHomepageViewController *controller;
@property (nonatomic, strong)   WPCLeftSidebarViewController *leftSidebarController;
@end

@implementation WPCHomepagePresenter

- (void)attachController:(UIViewController*)controller
{
    self.controller = (WPCHomepageViewController*)controller;
}

- (void)refreshHomepageWithTag:(NSString*)tag page:(NSInteger)page reload:(BOOL)reload callback:(completeCallback)callback
{
   [[WPCNetWorkingManager shareInstance] queryThemesInfoWithTag:tag page:page success:^(id responseObject) {
       WPCThemesPageModel *pageModel = [[WPCThemesPageModel alloc] initWithPage:page andItems:responseObject];
       if (!reload) {
           [_themesModel.pages addObject:pageModel];
           _themesModel.itemsCount += pageModel.items.count;
       }
       else {
           _themesModel = [[WPCThemesModel alloc] init];
           _themesModel.tag = tag;
           
           _themesModel.pages = [NSMutableArray arrayWithObject:pageModel];
           _themesModel.itemsCount += pageModel.items.count;
           _themesModel.totalPage = ((WPCThemesItem*)pageModel.items.firstObject).totalpage;
       }
       
       callback(YES);
   } failure:^(NSError *error) {
       callback(NO);
   }];
}

- (NSInteger)themesCount
{
    return (_themesModel ? _themesModel.itemsCount : 0);
}

- (CGSize)thumbnailSize
{
    if (nil == _themesModel) {
        return CGSizeMake(0, 0);
    }
    
    WPCThemesItem *item = [self themesItemAtIndex:0 pageOut:nil];
    return CGSizeMake([item.thumbnail[1] integerValue], [item.thumbnail[2] integerValue]);
}

- (NSInteger)totalPage
{
    return _themesModel.totalPage;
}

- (WPCThemesItem*)themesItemAtIndex:(NSInteger)index pageOut:(NSInteger*)pageOut
{
    if (nil == _themesModel || index >= _themesModel.itemsCount) {
        return nil;
    }
    
    WPCThemesItem *item = nil;
    int count = 0;
    for (int page = 0; page < _themesModel.pages.count; ++page) {
        WPCThemesPageModel *pageModel = _themesModel.pages[page];
        count += pageModel.items.count;
        if (count > index) {
            count -= pageModel.items.count;
            item = pageModel.items[index - count];
            if (pageOut) {
                *pageOut = page+1;
            }
            break;
        }
    }
    
    if (nil == item.thumbnailURL) {
        item.thumbnailURL = [WPCURLFactory getThumbnailURLWithTitle:item.title];
        item.originalPicURL = [WPCURLFactory getOriginalPicURLWithTitle:item.title];
    }
    
    return item;
}

- (BOOL)previewWallpaper:(NSInteger)index
{
    WPCThemesItem *item = [self themesItemAtIndex:index pageOut:nil];
    WPCPreviewViewController *controller = [[WPCPreviewViewController alloc] initWithThemesItem:item];
    
    /*设置动画自定义*/
    _controller.animatedType = FROME_RIGHT_TO_LEFT;
    controller.transitioningDelegate = _controller;
    [_controller presentViewController:controller animated:YES completion:nil];    
    return YES;
}

- (BOOL)showLeftSidebar
{
    if (nil == _leftSidebarController) {
        _leftSidebarController = [[WPCLeftSidebarViewController alloc] init];
        [_leftSidebarController setDelegateWithDelegate:_controller];
    }
    
    /*设置动画自定义*/
    _controller.animatedType = FROME_LEFT_TO_RIGHT;
    _leftSidebarController.transitioningDelegate = _controller;
    [_controller presentViewController:_leftSidebarController animated:YES completion:nil];
    return YES;
}

@end
